using MediatR;
using Microsoft.Data.SqlClient;
using PiggyBankCashFlowApi.Delegate.command;
using PiggyBankCashFlowApi.Exception;
using PiggyBankCashFlowApi.Repo;
using PiggyBankCashFlowApi.Repo.Entity;

namespace PiggyBankCashFlowApi.Delegate.Handler;

public class QueryCashFlowByIdHandler(CashFlowRepo repo, ILogger<QueryCashFlowByIdHandler> logger)
    : IRequestHandler<QueryCashFlowByIdCommand, CashFlowEntity>
{
    public async Task<CashFlowEntity> Handle(QueryCashFlowByIdCommand request, CancellationToken cancellationToken)
    {
        var entity = await repo.GetCashFlowAsync(request.Id);
        return entity ?? throw new ResourceNotFoundException($"Cannot find cash flow with Id: {request.Id}");
    }
}